package com.xdwanj.smartcity.data.repository.impl;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.xdwanj.smartcity.App;
import com.xdwanj.smartcity.data.bean.BannerData;
import com.xdwanj.smartcity.data.bean.News;
import com.xdwanj.smartcity.data.bean.result.BannerResult;
import com.xdwanj.smartcity.data.bean.result.NewsResult;
import com.xdwanj.smartcity.data.config.Configs;
import com.xdwanj.smartcity.data.repository.IHomeRepository;
import com.xdwanj.smartcity.util.EncodedUtils;
import com.xdwanj.smartcity.util.StringUtils;
import com.xdwanj.smartcity.util.UrlUtils;

import java.util.Date;
import java.util.List;

public class HomeRepository implements IHomeRepository {
    private static final String TAG = "HomeRepository";
    private static HomeRepository INSTANCE = null;

    private HomeRepository() {
    }

    public static HomeRepository getInstance() {
        if (INSTANCE != null)
            return INSTANCE;
        else
            throw new IllegalStateException("not init");
    }

    public static void init() {
        INSTANCE = new HomeRepository();
    }

    @Override
    public void cancelAll() {
        App.queue.cancelAll(TAG);
    }

    @Override
    public LiveData<List<News>> listNews(String hot, Date publishDate, String title, String top, String type) {
        MutableLiveData<List<News>> data = new MutableLiveData<>();
        String url = "/prod-api/press/press/list?aaa";

        if (!StringUtils.isNullOrEmpty(hot)) {
            url = UrlUtils.urlAppend(url, "hot", hot);
        }
        if (publishDate != null) {
            url = UrlUtils.urlAppend(url, "publishDate", publishDate.getTime() + "");
        }
        if (!StringUtils.isNullOrEmpty(title)) {
            url = UrlUtils.urlAppend(url, "title", title);
        }
        if (!StringUtils.isNullOrEmpty(top)) {
            url = UrlUtils.urlAppend(url, "top", top);
        }
        if (!StringUtils.isNullOrEmpty(type)) {
            url = UrlUtils.urlAppend(url, "type", type);
        }
        StringRequest request = new StringRequest(Request.Method.GET, Configs.BASE_URL + url,
                s -> {
                    String str = EncodedUtils.remoteToLocal(s);
                    str = StringUtils.replaceByImg(str, Configs.BASE_URL);
                    System.out.println("str = " + str);
                    data.setValue(App.gson.fromJson(str, NewsResult.class).getRows());
                },
                volleyError -> Log.d(TAG, "onErrorResponse: " + volleyError)
        );
        request.setTag(TAG);
        request.setShouldCache(false);
        App.queue.add(request);
        return data;
    }

    @Override
    public LiveData<List<BannerData>> listBannerByType(int type, int pageNum, int pageSize) throws Exception {
        StringBuffer url = new StringBuffer("/prod-api/api/rotation/list?");
        if (type > 2 || type < 1) {
            type = 1;
        }
        url.append("type=").append(type);

        if (pageNum != 0) {
            url.append("&pageNum=").append(pageNum);
        }

        if (pageSize != 0) {
            url.append("&pageSize=").append(pageSize);
        }

        MutableLiveData<List<BannerData>> data = new MutableLiveData<>();
        StringRequest request = new StringRequest(Request.Method.GET,
                Configs.BASE_URL + url,
                s -> {
                    String str = EncodedUtils.remoteToLocal(s);
                    str = StringUtils.replaceByImg(str, Configs.BASE_URL);
                    System.out.println("str = " + str);
                    BannerResult bannerResult = App.gson.fromJson(str, BannerResult.class);
                    data.setValue(bannerResult.getRows());
                },
                volleyError -> Log.d(TAG, "onErrorResponse: " + volleyError)
        );
        request.setTag(TAG);
        App.queue.add(request);
        return data;
    }
}
